﻿using System;
using Grundy.Message;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Grundy.Client.Tests
{
    [TestClass]
    public class CheckinTests
    {

        [TestMethod]
        public void Checkin_uncounted_floating_license()
        {
            var client = CheckoutTests.GetLicenseClient();
            const string FEATURE_NAME = "Dev_Uncounted_Floating";
            var response = client.Checkout(new FeatureIdentificationKey { FeatureName = FEATURE_NAME });
            response = client.Checkin(response);
            Assert.IsTrue(response.IsSuccessful);
            Assert.AreEqual(response.FeatureIdentificationKey.FeatureName, FEATURE_NAME);
        }

        [TestMethod]
        public void Checkin_counted_floating_concurrent_1_license()
        {
            var client = CheckoutTests.GetLicenseClient();
            var FEATURE_NAME = "Dev_Counted_Floating_Concurrent_1";
            var response = client.Checkout(new FeatureIdentificationKey { FeatureName = FEATURE_NAME });
            Assert.IsTrue(response.IsSuccessful);
            response = client.Checkin(response);
            Assert.IsTrue(response.IsSuccessful);
            Assert.AreEqual(response.FeatureIdentificationKey.FeatureName, FEATURE_NAME);
        }

        [TestMethod]
        public void Checkout_should_be_successful_after_checkin_for_counted_floating_concurrent_1_license()
        {
            var client = CheckoutTests.GetLicenseClient();
            var FEATURE_NAME = "Dev_Counted_Floating_Concurrent_1";
            var response = client.Checkout(new FeatureIdentificationKey { FeatureName = FEATURE_NAME });
            Assert.IsTrue(response.IsSuccessful);
            var response1 = client.Checkout(new FeatureIdentificationKey { FeatureName = FEATURE_NAME });
            Assert.IsFalse(response1.IsSuccessful);
            response = client.Checkin(response);
            Assert.IsTrue(response.IsSuccessful);
            Assert.AreEqual(response.FeatureIdentificationKey.FeatureName, FEATURE_NAME);
            response = client.Checkout(new FeatureIdentificationKey { FeatureName = FEATURE_NAME });
            Assert.IsTrue(response.IsSuccessful);

            // Cleanup
            client.Checkin(response);
        }

        [TestMethod]
        public void Checkin_node_locked_license()
        {
            var client = CheckoutTests.GetLicenseClient();
            var response = client.Checkout(new FeatureIdentificationKey { FeatureName = "Dev_Node_Locked" });
            response = client.Checkin(response);
            Assert.IsTrue(response is LocalLicenseResponse);
            Assert.IsTrue(response.IsSuccessful);
        }
    }
}